package com.ruoyi.movie.service.impl;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.movie.mapper.MovieBasicToColumnMapper;
import com.ruoyi.movie.domain.MovieBasicToColumn;
import com.ruoyi.movie.service.IMovieBasicToColumnService;

/**
 * 影视栏目关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class MovieBasicToColumnServiceImpl implements IMovieBasicToColumnService
{
    @Autowired
    private MovieBasicToColumnMapper movieBasicToColumnMapper;

    /**
     * 查询影视栏目关联
     *
     * @param movieBasicToColumnId 影视栏目关联主键
     * @return 影视栏目关联
     */
    @Override
    public MovieBasicToColumn selectMovieBasicToColumnById(Integer movieBasicToColumnId)
    {
        return movieBasicToColumnMapper.selectMovieBasicToColumnById(movieBasicToColumnId);
    }

    /**
     * 查询影视栏目关联列表
     *
     * @param movieBasicToColumn 影视栏目关联
     * @return 影视栏目关联
     */
    @Override
    public List<MovieBasicToColumn> selectMovieBasicToColumnList(MovieBasicToColumn movieBasicToColumn)
    {
        return movieBasicToColumnMapper.selectMovieBasicToColumnList(movieBasicToColumn);
    }

    /**
     * 根据影视ID查询关联的栏目ID列表
     *
     * @param movieBasicsId 影视基础ID
     * @return 栏目ID集合
     */
    @Override
    public List<Integer> selectColumnIdsByMovieBasicsId(Integer movieBasicsId)
    {
        return movieBasicToColumnMapper.selectColumnIdsByMovieBasicsId(movieBasicsId);
    }

    /**
     * 新增影视栏目关联
     *
     * @param movieBasicToColumn 影视栏目关联
     * @return 结果
     */
    @Override
    public int insertMovieBasicToColumn(MovieBasicToColumn movieBasicToColumn)
    {
        return movieBasicToColumnMapper.insertMovieBasicToColumn(movieBasicToColumn);
    }

    /**
     * 批量新增影视栏目关联
     *
     * @param movieBasicsId 影视基础ID
     * @param columnIds 栏目ID数组
     * @return 结果
     */
    @Override
    public int batchInsertMovieBasicToColumn(Integer movieBasicsId, Integer[] columnIds)
    {
        if (columnIds == null || columnIds.length == 0)
        {
            return 0;
        }

        List<MovieBasicToColumn> list = new ArrayList<>();
        for (Integer columnId : columnIds)
        {
            MovieBasicToColumn relation = new MovieBasicToColumn();
            relation.setMovieBasicsId(movieBasicsId);
            relation.setColumnId(columnId);
            list.add(relation);
        }

        return movieBasicToColumnMapper.batchInsertMovieBasicToColumn(list);
    }

    /**
     * 修改影视栏目关联
     *
     * @param movieBasicToColumn 影视栏目关联
     * @return 结果
     */
    @Override
    public int updateMovieBasicToColumn(MovieBasicToColumn movieBasicToColumn)
    {
        return movieBasicToColumnMapper.updateMovieBasicToColumn(movieBasicToColumn);
    }

    /**
     * 批量删除影视栏目关联
     *
     * @param movieBasicToColumnIds 需要删除的影视栏目关联主键
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToColumnByIds(Integer[] movieBasicToColumnIds)
    {
        return movieBasicToColumnMapper.deleteMovieBasicToColumnByIds(movieBasicToColumnIds);
    }

    /**
     * 删除影视栏目关联信息
     *
     * @param movieBasicToColumnId 影视栏目关联主键
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToColumnById(Integer movieBasicToColumnId)
    {
        return movieBasicToColumnMapper.deleteMovieBasicToColumnById(movieBasicToColumnId);
    }

    /**
     * 根据影视ID删除关联
     *
     * @param movieBasicsId 影视基础ID
     * @return 结果
     */
    @Override
    public int deleteMovieBasicToColumnByMovieBasicsId(Integer movieBasicsId)
    {
        return movieBasicToColumnMapper.deleteMovieBasicToColumnByMovieBasicsId(movieBasicsId);
    }

    /**
     * 保存影视栏目关联（先删除旧的，再批量插入新的）
     *
     * @param movieBasicsId 影视基础ID
     * @param columnIds 栏目ID数组
     * @return 结果
     */
    @Override
    public int saveMovieBasicToColumn(Integer movieBasicsId, Integer[] columnIds)
    {
        // 先删除旧的关联
        deleteMovieBasicToColumnByMovieBasicsId(movieBasicsId);

        // 再批量插入新的关联
        return batchInsertMovieBasicToColumn(movieBasicsId, columnIds);
    }
}
